package SmartGrid;

import java.util.Random;

import Plants.PowerPlant;

/**
 * Household device that can monitor a house's energy level and control
 * a house's appliances.
 * @author jeffrey
 *
 */

public class ConsumerEnergyMeter {

	private PowerPlant location;  //a pointer to the power plant that is responsible for providing this consumer with energy
	private int energyDemand;
	
	private Random random;
	
	
	
	/**
	 * Represents a consumer and his/her energy demand
	 */
	public ConsumerEnergyMeter()
	{
		random = new Random();
		energyDemand = Tools.Options.INIT_CONSUMER_DEMAND;
	}
	
	/**
	 * Create a consumer with a fixed energy demand
	 * @param energyDemand
	 */
	public ConsumerEnergyMeter(int energyDemand)
	{
		this();
		this.energyDemand = energyDemand;
	}
	
	/**
	 * Creates a consumer that is being provided energy by a specific plant
	 * @param plant the power plant that is responsible for providing energy to this consumer
	 */
	public ConsumerEnergyMeter(PowerPlant plant)
	{
		this();
		location = plant;
	}
	
	/**
	 * Creates a consumer with fixed energy demand.
	 * @param plant the power plant that is responsible for providing energy to this consumer
	 * @param energyDemand
	 */
	public ConsumerEnergyMeter(PowerPlant plant, int energyDemand)
	{
		this(energyDemand);
		location = plant;
	}
	
	/**
	 * Change the energy demanded by this consumer by setting it to a random integer
	 * Informs the power plant about its added/reduced responsibilities, but no action is immediately taken
	 * to adjust the plant's electric output
	 * Triggers a repaint in the canvas
	 * @return the amount by which the energy demanded CHANGED, not the new demand level
	 */
	public int changeDemand()
	{
		int oldDemand = energyDemand;
		energyDemand = random.nextInt(100);
		int delta = energyDemand - oldDemand;
		location.setResponsibilities(location.getResponsibilities() + delta, true);
		return delta;
	}
	
	public int getEnergyDemand()
	{
		return energyDemand;
	}

	public PowerPlant getLocation() {
		return location;
	}

	public void setLocation(PowerPlant location) {
		this.location = location;
	}

	public void setEnergyDemand(int energyDemand) {
		this.energyDemand = energyDemand;
	}
	
	
}
